package com.fingard.text;

import java.util.HashMap;

public class SplitHelper {
	
	/**拆分出各字段值map*/
	public static HashMap<String,String> splitAsValueMap(String p_columnStr, String p_valueStr, String p_SpChar) throws Exception{
		HashMap<String, String> retMap = new HashMap<String,String>();
		String[] columns = p_columnStr.split(p_SpChar);
		String[] values = p_valueStr.split(p_SpChar);
		if(columns.length == values.length){
			for(int i=0;i<values.length;i++){
				retMap.put(columns[i],values[i]);
			}
		}
		else{
			throw new Exception("字段列数与值列数不相等");
		}
		return retMap;
    }

	/**拆分出各字段值map，本方法空值也拆分，例如："A|B|C||" 以"\\|"拆分得到 {"A","B","C","",""} limit的值要大于数组的长度即可*/
	public static HashMap<String,String> splitAsValueMap(String p_columnStr, String p_valueStr, String p_SpChar, int limit) throws Exception{
		HashMap<String, String> retMap = new HashMap<String,String>();
		String[] columns = p_columnStr.split(p_SpChar,limit);
		String[] values = p_valueStr.split(p_SpChar,limit);
		if(columns.length == values.length){
			for(int i=0;i<values.length;i++){
				retMap.put(columns[i],values[i]);
			}
		}
		else{
			throw new Exception("字段列数与值列数不相等");
		}
		return retMap;
	}
	
	/**拆分出各字段值map*/
	public static HashMap<String,String> splitAsValueMap(String[] p_columns, String[] p_values) throws Exception{
		HashMap<String, String> retMap = new HashMap<String,String>();
		if(p_columns.length == p_values.length){
			for(int i=0;i<p_values.length;i++){
				retMap.put(p_columns[i],p_values[i]);
			}
		}
		else{
			throw new Exception("字段列数与值列数不相等");
		}
		return retMap;
    }
}
